package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by root on 3/31/18.
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManageController {

    @Autowired
    private IUserService iUserService;


    @ResponseBody
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public ServerResponse login(HttpSession session, String userName, String password){
        ServerResponse<User> resp = iUserService.login(userName, password);
        if ( resp.isSuccess()){
            User user = resp.getData();
            if (Const.Role.ROLE_ADMIN != user.getRole()){
                return ServerResponse.createByError(" not an admin");
            }
            session.setAttribute(Const.CURRENT_USER, user);
            return ServerResponse.createBySuccess();
        }
        return resp;
    }
}
